package com.twu.biblioteca;

import java.util.Scanner;

public class CustomerMenu extends Menu {
    private static final int LIST_BOOKS = 1;
    private static final int CHECK_OUT_A_BOOK = 2;
    private static final int RETURN_A_BOOK = 3;
    private static final int LIST_MOVIES = 4;
    private static final int CHECK_OUT_A_MOVIE = 5;
    private static final int VIEW_USER_INFORMATION = 6;
    private static final int QUIT = 7;
    private Customer customer;

    public CustomerMenu(BookList availableBooks, BookList checkoutBooks, MovieList availableMovies, MovieList checkoutMovies, Customer customer) {
        this.availableBooks = availableBooks;
        this.checkoutBooks = checkoutBooks;
        this.availableMovies = availableMovies;
        this.checkoutMovies = checkoutMovies;
        this.customer = customer;
        optionDescriptions = new String[]{"List Books", "Check out a book", "Return a book", "List Movies", "Check out a movie", "View User Information", "Quit"};
    }

    @Override
    public Menu handleOption(int option) {
        if (selectAWrongOption(option)) {
            return this;
        } else {
            if (option == QUIT) {
                Menu menu = new TopMenu(this.availableBooks, this.checkoutBooks, this.availableMovies, this.checkoutMovies);
                return menu;
            } else if (option == LIST_BOOKS) {
                this.availableBooks.printBookList();
            } else if (option == CHECK_OUT_A_BOOK) {
                checkoutABook();
            } else if (option == RETURN_A_BOOK) {
                returnABook();
            } else if (option == LIST_MOVIES) {
                this.availableMovies.printMovieList();
            } else if (option == CHECK_OUT_A_MOVIE) {
                checkoutAMovie();
            } else {
                customer.printCustomerInfo();
            }
            return this;
        }
    }

    private void checkoutABook() {
        System.out.print("Checkout a book. ");
        int number = readInputBookNumber();

        if (!isBookAvailableToCheckout(number)) {
            System.out.println("*****************  That book is not available.  ****************\n");
            return;
        }
        removeBookToCheckoutBookListByNumber(number);
        System.out.println("*****************  Thank you! Enjoy the book.  *****************\n");
    }

    private int readInputBookNumber() {
        System.out.print("Please Input a Book Number: ");
        Scanner scanner = new Scanner(System.in);
        return scanner.nextInt();
    }

    private Boolean isBookAvailableToCheckout(int number) {
        if (this.availableBooks.getBookByNumber(number) == null) {
            return false;
        }
        return true;
    }

    private void removeBookToCheckoutBookListByNumber(int number) {
        Book checkoutBook = this.availableBooks.removeBookByNumber(number);
        checkoutBook.setLibraryNumber(this.customer.getLibraryNumber());
        this.checkoutBooks.add(checkoutBook);
    }

    private void returnABook() {
        System.out.print("Return a book. ");
        int number = readInputBookNumber();

        if (!isBookAvailableToReturn(number)) {
            System.out.println("*************  That is not a valid book to return.  ************\n");
            return;
        }
        returnBookToVisibleBookListByNumber(number);
        System.out.println("**************  Thank you for returning the book.  *************\n");
    }

    private Boolean isBookAvailableToReturn(int number) {
        if (this.checkoutBooks.getBookByNumber(number) == null) {
            return false;
        }
        return true;
    }

    private void returnBookToVisibleBookListByNumber(int number) {
        Book returnedBook = this.checkoutBooks.removeBookByNumber(number);
        returnedBook.setLibraryNumber(null);
        this.availableBooks.add(returnedBook);
    }

    private void checkoutAMovie() {
        System.out.print("Checkout a movie. Please Input a Movie Number: ");
        Scanner scanner = new Scanner(System.in);
        int number = scanner.nextInt();

        if (!isMovieAvailableToCheckout(number)) {
            System.out.println("*****************  That movie is not available.  ***************\n");
            return;
        }
        removeMovieToCheckoutMovieListByNumber(number);
        System.out.println("*****************  Thank you! Enjoy the movie.  ****************\n");
    }

    private Boolean isMovieAvailableToCheckout(int number) {
        if (this.availableMovies.getMovieByNumber(number) == null) {
            return false;
        }
        return true;
    }

    private void removeMovieToCheckoutMovieListByNumber(int number) {
        this.checkoutMovies.add(this.availableMovies.removeMovieByNumber(number));
    }
}
